package com.yiip.mr.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * wordcount 实例mapper类
 */
public class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {

	//公共的提取到外面去。
	Text text = new Text();
	IntWritable intWritable = new IntWritable(1);

	@Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1、获取一行
        String line = value.toString();

        //2、按照空格切割
        String[] words = line.split(" ");

        //3、往text里面输出
        intWritable.set(1);
        for (String word:words) {
            text.set(word);
            context.write(text,intWritable);
        }
    }
}
